package com.so206.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.so206.mapper.SystemSettMapper;
import com.so206.mapper.ext.SysSettExtMapper;
import com.so206.mapper.ext.SystemCountMapper;
import com.so206.po.SystemSett;
import com.so206.po.ext.SysSettExt;
import com.so206.service.SettLementService;
import com.so206.utils.BeanCheck;
import com.so206.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class SettLementServiceImpl implements SettLementService {

    @Autowired
    private SystemSettMapper mapper;

    @Autowired
    private SysSettExtMapper sysSettExtMapper;

    @Autowired
    private SystemCountMapper countMapper;

    @Override
    public Long saveSettLement(SystemSett sett) {
        mapper.insert(sett);
        return sett.getId();
    }

    @Override
    public PageInfo<SysSettExt> findByPage(Integer page, Integer rows, Integer status, String sid, Integer type, Long uid) {
        PageHelper.startPage(page, rows);
        if (sid != null && sid.length() == 0) {
            sid = null;
        }
        List<SysSettExt> list = sysSettExtMapper.findpage(sid, status, type, uid);
        return new PageInfo<>(list);
    }

    @Override
    public SystemSett find_sett_byId(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public SystemSett find_sett_by_uid(Long uid) {
        return mapper.selectByUid(uid);
    }

    @Override
    public void updateSystemSett(SystemSett sett) {
        SystemSett model = mapper.selectByPrimaryKey(sett.getId());
        if (model != null) {
            BeanCheck.copyPropertiesIgnoreNull(sett, model);
            mapper.updateByPrimaryKey(model);
        }
    }

    @Override
    public void deleteSystemSett(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<SystemSett> findsettByUid(Long uid) {
        return mapper.selectByUid2(uid);
    }

    @Override
    public List<SystemSett> findAllSett(Integer status) {
        return mapper.selectByStatus(status);
    }

    @Override
    public List<SystemSett> findAllSett2(Integer sett) {
        return mapper.selectByIsSett(sett);
    }

    @Override
    public Integer settCountAll(Long uid, String timelike, String end_time) {
        DateUtils utils = new DateUtils();
        try {
            utils.setStart_time(DateUtils.format.parse(timelike));
            utils.setEnd_time(DateUtils.format.parse(end_time));
        } catch (Exception e) {
            e.printStackTrace();
        }
        Date start_time = utils.getStartTime();
        Date stop_time = utils.getEndTime();
        List<SystemSett> setts = mapper.selectByUidTimes(uid, start_time, stop_time, null);
        if (setts != null) {
            return setts.size();
        }
        return 0;
    }

    @Override
    public Integer settPayOkCount(Long uid, String timelike, String end_time) {
        DateUtils utils = new DateUtils();
        try {
            utils.setStart_time(DateUtils.format.parse(timelike));
            utils.setEnd_time(DateUtils.format.parse(end_time));
        } catch (Exception e) {
            e.printStackTrace();
        }
        Date start_time = utils.getStartTime();
        Date stop_time = utils.getEndTime();
        List<SystemSett> setts = mapper.selectByUidTimes(uid, start_time, stop_time, new Integer[]{2});
        if (setts != null) {
            return setts.size();
        }
        return 0;
    }

    @Override
    public Integer settPayFaildCount(Long uid, String timelike, String end_time) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(3);
        DateUtils utils = new DateUtils();
        try {
            utils.setStart_time(DateUtils.format.parse(timelike));
            utils.setEnd_time(DateUtils.format.parse(end_time));
        } catch (Exception e) {
            e.printStackTrace();
        }
        Date start_time = utils.getStartTime();
        Date stop_time = utils.getEndTime();
        List<SystemSett> setts = mapper.selectByUidTimes(uid, start_time, stop_time, list.toArray(new Integer[list.size()]));
        if (setts != null) {
            return setts.size();
        }
        return 0;
    }

    @Override
    public Integer settCanelCount(Long uid, String timelike, String end_time) {

        DateUtils utils = new DateUtils();
        try {
            utils.setStart_time(DateUtils.format.parse(timelike));
            utils.setEnd_time(DateUtils.format.parse(end_time));
        } catch (Exception e) {
            e.printStackTrace();
        }
        Date start_time = utils.getStartTime();
        Date stop_time = utils.getEndTime();
        List<SystemSett> setts = mapper.selectByUidTimes(uid, start_time, stop_time, new Integer[]{4});
        if (setts != null) {
            return setts.size();
        }
        return 0;
    }

    @Override
    public BigDecimal settPayMoney(Long uid, Integer status, String timelike, String end_time) {
        DateUtils utils = new DateUtils();
        try {
            utils.setStart_time(DateUtils.format.parse(timelike));
            utils.setEnd_time(DateUtils.format.parse(end_time));
        } catch (Exception e) {

        }
        return countMapper.settPayMoney(uid, status, utils.getStartTime(), utils.getEndTime());
    }

    @Override
    public BigDecimal settPaySubMoney(Long uid, Integer status, String timelike, String end_time) {
        DateUtils utils = new DateUtils();
        try {
            utils.setStart_time(DateUtils.format.parse(timelike));
            utils.setEnd_time(DateUtils.format.parse(end_time));
        } catch (Exception e) {

        }
        return countMapper.settPaySubMoney(uid, status, utils.getStartTime(), utils.getEndTime());
    }

    @Override
    public BigDecimal findUserSettPayCount(Long uid) {
        BigDecimal ret = new BigDecimal("0.00");
        ArrayList<Integer> select = new ArrayList<>();
        select.add(1);
        select.add(2);
        select.add(3);
        List<SystemSett> list = mapper.selectByUidTimes(uid, null, null, select.toArray(new Integer[select.size()]));
        if (list != null && list.size() > 0) {
            for (SystemSett systemSett : list) {
                ret = ret.add(systemSett.getSettFinalMoney());
            }
        }
        return ret;
    }




}
